# 作者 :南雨
# 时间 : 2022/3/23 20:49
class FMM:
    def __init__(self):
        pass

    def cut_word(self, sentence, word_dict):
        word_len_word = [len(word) for word in word_dict]
        max_word_len = max(word_len_word)
        sentence_len = len(sentence)
        cut_word_list = []
        while sentence_len > 0:
            max_cut_len = min(max_word_len, sentence_len)
            word = sentence[0:max_cut_len]
            while max_cut_len > 0:
                if word in word_dict:
                    cut_word_list.append(word)
                    break
                if max_cut_len == 1:
                    # 当最大切分长度为1时，那么一个中文就是一个词，直接将词存入切分列表
                    cut_word_list.append(word)
                    break
                else:
                    max_cut_len = max_cut_len - 1
                    word = word[0:max_cut_len]
            sentence = sentence[max_cut_len:]
            sentence_len = sentence_len - max_cut_len
        return cut_word_list
